<?php

namespace app\common\service;


class ControlService extends BaseService
{
    /**
     * +----------------------------------------------------------
     * 获取管控方案名称
     * +----------------------------------------------------------
     * @author hongwei 2019-06-26
     * +----------------------------------------------------------
     */
    public function getControlPlanNumber()
    {
        //step1:获取目录
        $sql  = 'select * from control_plan_name where date_format(curdate(),"%Y-%m-%d") = "' . date('Y-m-d', time()) . '" order by id desc limit 1';
        $info = $this->pdoFetch($sql);
        return $info['plan_number'];
    }

    /**
     * +----------------------------------------------------------
     * 获取管控方案名称
     * +----------------------------------------------------------
     * @author hongwei 2019-06-26
     * +----------------------------------------------------------
     */
    public function getControlPlanInfo()
    {
        //step1:获取目录
        $sql  = 'select * from control_plan_name where date_format(curdate(),"%Y-%m-%d") = "' . date('Y-m-d', time()) . '" order by id desc limit 1';
        $info = $this->pdoFetch($sql);
        return $info;
    }

    /**
     * +----------------------------------------------------------
     * 获取清华方案够狠
     * +----------------------------------------------------------
     * @return mixed
     * @author hongwei 2019-06-26
     * +----------------------------------------------------------
     */
    public function getPlanTsingNameList($planNumber)
    {
        $sql    = 'select * from control_plan_from_tsinghua_plan where plan_number ="' . $planNumber . '" order by id asc';
        $result = $this->pdoFetchAll($sql);
        if (!$result) {
            return false;
        }
        return $result;
    }

    /**
     * +----------------------------------------------------------
     * 插入源清单
     * +----------------------------------------------------------
     * @param $data
     * @param $planNumber
     * @param $tsinghuaPlanName
     * @param $industryId
     * +----------------------------------------------------------
     * @author hongwei 2019-06-26
     * +----------------------------------------------------------
     */
    public function insertReductionSourceListDay($data, $planNumber, $tsinghuaPlanName, $industryId)
    {
        foreach ($data as $k => $v) {
            //插入数据
            $sql  = 'insert into bureau_source_list_reduction_day(type,plan_number,tsinghua_plan_name,industry_id,company_id,drain_name,longitude,latitude,nox_emissions,so2_emissions,tsp_emissions,create_time) values ';
            $sql  .= '(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
            $stmt = $this->pdo->prepare($sql);
            //绑定参数
            //基本数据
            $stmt->bindValue(1, 'day'); //类型：天
            $stmt->bindValue(2, $planNumber);  //预案编号
            $stmt->bindValue(3, $tsinghuaPlanName);//清华预案名称;
            $stmt->bindValue(4, $industryId);//行业ID
            $stmt->bindValue(5, $v['company_id']);   //公司ID号
            $stmt->bindValue(6, $v['drain_name']);  //排口号
            $stmt->bindValue(7, $v['latitude']);  //经度
            $stmt->bindValue(8, $v['longitude']);//纬度
            //排放量
            $stmt->bindValue(9, $v['emissions_nox'] * 1000000);   //nox的排放量,数据库存储mg/h
            $stmt->bindValue(10, $v['emissions_so2'] * 1000000); //so2的排放量,数据库存储mg/h
            $stmt->bindValue(11, $v['emissions_tsp'] * 1000000); //tsp的排放量,数据库存储mg/h
            //时间
            $stmt->bindValue(12, date('Y-m-d H:i:s', time())); //时间
            $stmt->execute();
        }
    }

    /**
     * 获取预案编号的结束日期距离开始日期的天数
     * @param string $planNumber 预案编号
     * @return float
     * @author hongwei 2019-07-10
     */
    public function getPlanNumberDays($planNumber)
    {
        //step1:查询sql
        $sql    = 'select * from control_plan_name where plan_number = "' . $planNumber . '"';
        $result = $this->pdoFetch($sql);

        //step2:获取相应字段
        $startDay = $result['control_start_time'];
        $endDay   = $result['control_end_time'];

        //step3:获取日期
        $days = ceil((strtotime($endDay) - strtotime($startDay)) / (3600 * 24));
        return $days;
    }

    /**
     * 获取预案编号开始日期
     * @param $planNumber
     * @return mixed
     * @author hongwei 2019-07-11
     */
    public function getPlanNumberStartDay($planNumber)
    {
        //step1:查询sql
        $sql    = 'select * from control_plan_name where plan_number = "' . $planNumber . '"';
        $result = $this->pdoFetch($sql);

        //step2:获取相应字段
        $startDay = strtotime($result['control_start_time']);
        $startDay = date('Y-m-d', $startDay);
        return $startDay;
    }

    /**
     * 获取预案编号结束日期
     * @param $planNumber
     * @return mixed
     * @author hongwei 2019-07-10
     */
    public function getPlanNumberEndDay($planNumber)
    {
        //step1:查询sql
        $sql    = 'select * from control_plan_name where plan_number = "' . $planNumber . '"';
        $result = $this->pdoFetch($sql);

        //step2:获取相应字段
        $endDay = strtotime($result['control_end_time']);
        $endDay = date('Y-m-d', $endDay);
        return $endDay;
    }

    /**
     * 获取确认的清华预案-通过预案编号
     * @param $planNumber
     * @return mixed
     * @throws \Exception
     */
    public function getConfirmTsinghuaPlanByPlanNumber($planNumber)
    {
        //step1:查询sql
        $sql    = 'select * from control_plan_from_tsinghua_plan where plan_number = "' . $planNumber . '" and confirm_scheme = 1';
        $result = $this->pdoFetch($sql);
        if (empty($result)) {
            throw new \Exception('根据预案编号获取清华确认方案！');
        }

        //step2:获取相应字段
        $confirmScheme = $result['name'];
        return $confirmScheme;
    }

    /**
     * 根据预案编号获取清华确认方案
     * @param $planNumber
     * @return bool 有值返回 true，无值返回 false
     * @throws \Exception
     * @author hongwei 2019-07-11
     */
    public function existConfirmTsinghuaPlanByPlanNumber($planNumber)
    {
        //step1:查询sql
        $sql    = 'select * from control_plan_from_tsinghua_plan where plan_number = "' . $planNumber . '" and confirm_scheme = 1';
        $result = $this->pdoFetch($sql);
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 管控前的天数固定是7天
     * @return int
     * @author hongwei 2019-07-24
     */
    public function getBeforeDays()
    {
        $days = 7;
        return (int)$days;
    }

    /**
     * 获取管控前的小时数
     * @author hongwei 2019-07-24
     * @return float|int
     */
    public function getBeforeHours()
    {
        $hours = $this->getBeforeDays() * 24;
        return $hours;
    }

    /**
     * 根据预案编号获取减排前结束时间
     * @param $planNumber
     * @return false|string
     * @author hongwei 2019-07-24
     */
    public function getBeforeEndTime($planNumber)
    {
        $controlStartTime = $this->getControlStartTime($planNumber);
        $beforeEndTimeStr = strtotime($controlStartTime) - 3600 * 24;
        $beforeEndTime    = date('Y-m-d', $beforeEndTimeStr);
        return $beforeEndTime;
    }

    /**
     * 根据预案编号获取减排前开始时间
     * @param $planNumber
     * @return false|string
     * @author hongwei 2019-07-24
     */
    public function getBeforeStartTime($planNumber)
    {
        $beforeEndTime      = $this->getBeforeEndTime($planNumber);
        $days               = $this->getBeforeDays();
        $beforeStartTimeStr = strtotime($beforeEndTime) - 3600 * 24 * $days;
        $beforeStartTime    = date('Y-m-d', $beforeStartTimeStr);
        return $beforeStartTime;
    }

    /**
     * 显示撩排前的所有小时，共168小时
     * @param $planNumber
     * @return array
     */
    public function getBeforeEveryHours($planNumber)
    {
        $start = $this->getBeforeStartTime($planNumber).' 00:00:00';
        $num = $this->getBeforeHours();
        $newData = [];
        for ($i = 0; $i < $num; $i++) {
            $newData[$i] =  date('Y-m-d H:i', strtotime($start) + 3600 * $i);
        }
        return $newData;
    }

    /**
     * 显示撩排前的所有小时，共7天
     * @param $planNumber
     * @return array
     */
    public function getBeforeEveryDays($planNumber)
    {
        $start = $this->getBeforeStartTime($planNumber).' 00:00:00';
        $num = $this->getBeforeDays();
        $newData = [];
        for ($i = 0; $i < $num; $i++) {
            $newData[$i] =  date('Y-m-d', strtotime($start) + 3600 * 24 * $i);
        }
        return $newData;
    }

    /**
     * 管控管控开始时间
     * @param $planNumber
     * @return mixed
     * @example "2019-08-01 00:00:00"
     * @author hongwei 2019-007-16
     */
    public function getControlStartTime($planNumber)
    {
        $sql    = 'select control_start_time from control_plan_name where plan_number = "' . $planNumber . '"';
        $result = $this->pdoFetchColumn($sql);
        return $result;
    }

    /**
     * 管控管控结束时间
     * @param $planNumber
     * @return mixed
     * @example "2019-08-04 23:59:59"
     * @author hongwei 2019-007-16
     */
    public function getControlEndTime($planNumber)
    {
        $sql    = 'select control_end_time from control_plan_name where plan_number = "' . $planNumber . '"';
        $result = $this->pdoFetchColumn($sql);
        return $result;
    }

    /**
     * 管控管控现在的结束时间
     * @param $planNumber
     * @return mixed
     * @example "2019-08-04 23:59:59"
     * @author hongwei 2019-007-16
     */
    public function getControlDoingEndTime()
    {
        $result = date('Y-m-d H:i:s', time());
        return $result;
    }

    /**
     * 获取管控天数
     * @param $planNumber
     * @return float
     * @author hongwei 2019-07-16
     */
    public function getControlDays($planNumber)
    {
        $endTime   = $this->getControlEndTime($planNumber);
        $startTime = $this->getControlStartTime($planNumber);
        $days      = (strtotime($endTime) - strtotime($startTime)) / (3600 * 24);
        return ceil($days);
    }

    /**
     * 获取管控期间的分钟数，从开始管控日期到现在
     * @param $planNumber
     * @return float
     * @author hongwei 2019-07-16
     */
    public function getControlDoingMinutes($planNumber)
    {
        $endTime   = time();
        $startTime = $this->getControlStartTime($planNumber);
        $days      = ($endTime - strtotime($startTime)) / 60;
        return ceil($days);
    }

    /**
     * 获取管控的每天
     * @author hongwei 2019-07-31
     * @param $planNumber
     * @return array
     * @example [['publish_time'=>'2019-08-01'],['publish_time'=>'2019-08-02'],['publish_time'=>'2019-08-03']]
     */
    public function getControlEveryDay($planNumber)
    {
        $startTime = $this->getControlStartTime($planNumber);
        $days = $this->getControlDays($planNumber);
        $data = [];
        for ($i = 0; $i <$days; $i++) {
            $data[$i]['publish_time'] = date('Y-m-d', strtotime($startTime) + $i * 3600 * 24);
        }
        return $data;
    }

    /**
     * 获取管控的所有每小时
     * @author hongwei 2019-07-31
     * @param $planNumber
     * @return array
     * @example [['publish_time'=>'2019-08-01 01:00'],['publish_time'=>'2019-08-01 02:00'],['publish_time'=>'2019-08-01 03:00']]
     */
    public function getControlEveryHour($planNumber)
    {
        $startTime = $this->getControlStartTime($planNumber);
        $days = $this->getControlDays($planNumber);
        $data = [];
        for ($i = 0; $i <$days * 24; $i++) {
            $data[$i]['publish_time'] = date('Y-m-d H:i', strtotime($startTime) + $i * 3600);
        }
        return $data;
    }

    /**
     * 获取管控有效的每小时
     * @author hongwei 2019-07-31
     * @param $planNumber
     * @return array
     * @example [['publish_time'=>'2019-08-01 01:00'],['publish_time'=>'2019-08-01 02:00'],['publish_time'=>'2019-08-01 03:00']]
     */
    public function getControlValidEveryHour($planNumber)
    {
        $startTime = $this->getControlStartTime($planNumber);
        $startTimeStr = strtotime($startTime);
        $intervalTime = time() - $startTimeStr;
        $hoursNumber  = ceil($intervalTime / 3600);
        $data = [];
        for ($i = 0; $i <(int)$hoursNumber; $i++) {
            $data[$i]['publish_time'] = date('Y-m-d H:i', strtotime($startTime) + $i * 3600);
        }
        return $data;
    }

    /**
     * 获取管控清单企业的ID
     * @param string $planNumber 预案编号
     * @return bool|mixed
     * @author hongwei 2019-07-17
     */
    public function getControlListCompanyIds($planNumber)
    {
        $sql    = 'select company_id from control_setting_list where plan_number = "' . $planNumber . '" group by company_id';
        $result = $this->pdoFetchAll($sql);
        $newArr = [];
        foreach ($result as $k => $v) {
            array_push($newArr, (int)$v['company_id']);
        }
        if (!$result) {
            return false;
        }
        $companyIds = implode(",", $newArr);
        return $companyIds;
    }

    /**
     * 获取管控清单排口的ID
     * @param string $planNumber 预案编号
     * @return bool|mixed
     * @author hongwei 2019-07-17
     */
    public function getControlListDrainsIds($planNumber)
    {
        $sql    = 'select drain_id from control_setting_list where plan_number = "' . $planNumber . '" group by company_id';
        $result = $this->pdoFetchAll($sql);
        $newArr = [];
        foreach ($result as $k => $v) {
            array_push($newArr, (int)$v['drain_id']);
        }
        if (!$result) {
            return false;
        }
        $companyIds = implode(",", $newArr);
        return $companyIds;
    }

    /**
     * +----------------------------------------------------------
     * 获取清华全部预案
     * +----------------------------------------------------------
     * @return mixed
     * @author hongwei 2019-08-07
     * +----------------------------------------------------------
     */
    public function getAllTsingNameList()
    {
        $sql    = 'select * from control_plan_from_tsinghua_plan order by id asc';
        $result = $this->pdoFetchAll($sql);
        if (!$result) {
            return false;
        }
        return $result;
    }

    /**
     * 发现预案编号相关的清单
     * @author hongwei 2019-08-08
     * @param $planNumber
     * @return array|bool
     */
    public function findSettingList($planNumber)
    {
        $sql = 'select * from control_setting_list where plan_number = "'. $planNumber .'"';
        $result = $this->pdoFetchAll($sql);
        if (!$result) {
            return false;
        }
        return true;
    }

    /**
     * 发现第二步是否生成数据
     * @author hongwei 2019-08-08
     * @param $planNumber
     * @param $tsinghuaPlanName
     * @return bool
     */
    public function findControlStepTwoGetDrainEmissions($planNumber, $tsinghuaPlanName)
    {
        $sql = 'select * from control_step_two_get_drain_emissions where plan_number = "'. $planNumber .'" and tsinghua_plan_name = "'.$tsinghuaPlanName. '"';
        $result = $this->pdoFetchAll($sql);
        if (!$result) {
            return false;
        }
        return true;
    }

    /**
     * 发现清华数据是否有排放量
     * @author hongwei 2019-08-08
     * @param $planNumber
     * @param $tsinghuaPlanName
     * @return bool
     */
    public function findTsinghuaGetDrainEmissions($planNumber, $tsinghuaPlanName)
    {
        $sql = 'select * from control_plan_from_tsinghua_drain_details where plan_number = "'. $planNumber .'" and tsinghua_plan_name = "'.$tsinghuaPlanName. '"';
        $result = $this->pdoFetchAll($sql);
        if (!$result) {
            return false;
        }
        return true;
    }

    /**
     * 发现管控第二部是否有数据
     * @param $planNumber
     * @param $facilityId
     * @param $tsinghuaPlanName
     * @return bool
     */
    public function findControlStepTwoGetFacilityParams($planNumber, $facilityId, $tsinghuaPlanName)
    {
        $sql = 'select * from control_step_two_get_facility_params where plan_number = "'. $planNumber .'" and tsinghua_plan_name = "'.$tsinghuaPlanName. '" and facility_id ='.$facilityId;
        $result = $this->pdoFetchAll($sql);
        if (!$result) {
            return false;
        }
        return true;
    }

    /**
     * 获取管控列表
     * @author hongwei 2019-08-09
     * @return bool
     */
    public function getControlList()
    {
        $sql = 'select * from control_plan_name order by id asc';
        $result = $this->pdoFetchAll($sql);
        if (!$result) {
            return false;
        }
        return true;
    }
}